// pages/goodList/goodList.js
import mode from '../../utils/mode.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerId:'',
        isBanner:0,
        isGood:0,
        goodsId:'',
        goodsList:[],   //商品列表
        user_id:'',
        userInfo:{},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(options.isGood){  //好物推荐
            this.setData({
                isGood:1
            })
        }
        if(options&&options.isBanner==1){   //轮播图
            wx.setNavigationBarTitle({
              title: '商品列表',
            })
            this.setData({
                isBanner:1,
                bannerId:options.bannerId
            })
        }else if(options.menuName&&options.menuName!='null'){
            wx.setNavigationBarTitle({
              title: options.menuName||'好物推荐',
            })
        }
    },
    async goodsThing () {
        wx.showLoading()
        let that = this
        let { code, data } = await wx.$api.goodsThing()
        if (code == 1) {
            let uInfo = that.data.userInfo
            let goods = data.goods||[]
                if(goods.length>0){
                    goods.forEach(v => {
                        if (uInfo && uInfo.consumption_discount &&  uInfo.consumption_discount<10) {
                            let oldP = v.retail_price * uInfo.consumption_discount / 10 * 1
                            v.vip_price = oldP.toFixed(2)
                        }else{
                            v.vip_price = v.retail_price
                        }
                    })
                    wx.hideLoading()
                    that.setData({
                        goodsList: goods
                    })
                }else{
                    that.setData({
                        goodsList: []
                    })
                    wx.hideLoading()
                    wx.showToast({
                      title: '商品信息为空',
                      icon:'error'
                    })
                }
        }
    },
    async bannerGoods () {
        wx.showLoading()
        let that = this
        let { code, data } = await wx.$api.getBannerInfo({id:that.data.bannerId})
        if (code == 1) {
            let uInfo = that.data.userInfo
            let goods = data.goods||[]
                if(goods.length>0){
                    goods.forEach(v => {
                        if (uInfo && uInfo.consumption_discount &&  uInfo.consumption_discount<10) {
                            let oldP = v.retail_price * uInfo.consumption_discount / 10 * 1
                            v.vip_price = oldP.toFixed(2)
                        }else{
                            v.vip_price = v.retail_price
                        }
                    })
                    wx.hideLoading()
                    that.setData({
                        goodsList: goods
                    })
                }else{
                    that.setData({
                        goodsList: []
                    })
                    wx.hideLoading()
                    wx.showToast({
                      title: '商品信息为空',
                      icon:'error'
                    })
                }
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var info = wx.getStorageSync('uinfo')
        if (info && info.user) {
            this.setData({
                userInfo: info.user,
                user_id: info.user.id || ''
            })
        }
        if(this.data.isGood<1){
            this.bannerGoods()
        }else{
            this.goodsThing()
        }
        
    },
    async getLists () {   //获取商品列表
        wx.showLoading({
          title: '加载中',
        })
        let that = this
        let ids = that.data.goodsId
        await wx.$api.goodsLists({
              goods_ids:ids
            }).then(res=>{
                let uInfo = that.data.userInfo
                let data = res.data
                if(data.length>0){
                    data.forEach(v => {
                        if (uInfo && uInfo.consumption_discount &&  uInfo.consumption_discount<10) {
                            let oldP = v.retail_price * uInfo.consumption_discount / 10 * 1
                            v.vip_price = oldP.toFixed(2)
                        }else{
                            v.vip_price = v.retail_price
                        }
                    })
                    wx.hideLoading()
                    if (res.code) {
                        that.setData({
                            goodsList: data
                        })
                    }
                }else{
                    wx.hideLoading()
                    wx.showToast({
                      title: '商品信息为空',
                      icon:'error'
                    })
                }
          })
        
    },
    goDetail (e) {  //去详情页
        let index = e.currentTarget.dataset.index
        let goodId = e.currentTarget.dataset.goodid
        console.log('tiaozhuan ', goodId)
        wx.navigateTo({
            url: '/pages/product-details/product-details?goodId=' + goodId
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})